<?php
//TODO: pääsynhallinta

include_once dirname(__FILE__).'/templates/header.php';


echo "<h3>".`whoami`."</h3>";
echo "<h3>moi</h3>";
// $fh = fopen("templates/header.php", 'r');
// if($fh) {
// 	echo "Tiedoston avaus onnistui: $fh\n";
// 	$i = 0;
// 	echo "<textarea cols='90' rows='20'>\n";
// 	while($i < 1000 && $rivi = fgets($fh)) {
// 		echo $rivi."\n";
// 		$i++;
// 	}
// 	echo "</textarea>\n";
// }else {
// 	echo "Avaus epäonnistui: ";
// 	print_r(error_get_last());
// }
?>

<script type="text/javascript">
function unifyTableWidths(tableClassName) {
	var tables = document.getElementsByTagName("table");
	var maxWidths = new Array();
	for (var t=0;t<tables.length;t++) {
		var rows = tables[t].getElementsByTagName("tr");
		for (var r=0;r<rows.length;r++ in rows) {
			var cells = rows[r].getElementsByTagName("td");
			for(var i=0; i<cells.length; i++) {
				if(!maxWidths[i] || maxWidths[i] < cells[i].offsetWidth)
					maxWidths[i] = cells[i].offsetWidth;
			}
		}
	}
	for (var t=0;t<tables.length;t++) {
		var firstrow = tables[t].getElementsByTagName("tr")[0].getElementsByTagName("td");
		for(var i=0; i<firstrow.length;i++) {
			firstrow[i].style.width = maxWidths[i] + "px";
		}
	}
	var str = "leveydet:\n";
	for (var i=0;i<maxWidths.length;i++) {
		str += " "+maxWidths[i];
	}
	alert(str);
}
window.onload = function() {
	//unifyTableWidths("tableOuter");
};
</script>
<?php
if(isset($_FILES)) {
	foreach($_FILES as $k => $v) {
		echo "<h4>Ladattiin tiedosto $k</h4>";
		foreach($v as $attrName => $attrVal) {
			echo "<h6 style='margin-left: 10px'>$attrName: $attrVal</h6>";
			if($attrName == "tmp_name") {
				$fileh = fopen($attrVal, "r");
				if(!$fileh) {
					echo "<h4>Virhe: ";
					print_r(error_get_last());
					echo "</h4>";
				}else {
                  //ladattu tiedosto saatiin avattua
					$rowCounter = 0;
					$totalRowCounter = 0;
					echo "<div class='tableOuter'>\n<table>";
					while($row = fgets($fileh)) {
                        $quotedStrings = array();
                        if(preg_match_all('/"[^"]*;[^"]*"[^;];/', $row, $quotedStrings) > 0) {
                          $row = preg_replace($pattern, $replacement, $subject)
                        }
						$cells = explode(";", $row);
						echo "<tr>\n<td>".($totalRowCounter + 1)."</td>";
						foreach($cells as $c) {
                          if(preg_match('/"/', $c)) {
                            echo "<td class='tableCell'><STRONG>LÖYTYI ;-MERKKI!</STRONG></td>";
                          }else {
							echo "<td class='tableCell'>$c</td>";
                          }
						}
						echo "</tr>\n";
						$totalRowCounter++;
					}
                    echo "</table>\n";
					echo "</div>\n";
				}
			}
		}
	}
	print_r(error_get_last());
}else {
	echo "<h3>Ei ladattuja tiedostoja</h3>";
}

?>
<form action="<?php echo basename(__FILE__) ?>" method="post" enctype="multipart/form-data" accept-charset="utf-8">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo return_bytes(ini_get('upload_max_filesize')) ?>" />
	<table>
		<tr><td>Tiedoston max-koko</td><td><?php echo return_bytes(ini_get('upload_max_filesize')) ?></td></tr>
		<tr><td>POSTin max-koko</td><td><?php echo ini_get('post_max_size') ?></td></tr>
		<tr><td></td><td></td></tr>
		<tr><td>Tiedosto</td><td><input type="file" name="tiedosto" /></td></tr>
		<tr><td></td><td><input type="submit" value="Lataa tiedosto" /></td></tr>
	</table>
</form>

<?php

include_once dirname(__FILE__).'/templates/footer.php';


function return_bytes($val) {
	$val = trim($val);
	$last = strtolower($val[strlen($val)-1]);
	switch($last) {
		// The 'G' modifier is available since PHP 5.1.0
		case 'g':
			$val *= 1024;
		case 'm':
			$val *= 1024;
		case 'k':
			$val *= 1024;
	}
	return $val;
}
